package ro.ubbcluj.cs.damate.plugin.events;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lorand Fazakas
 */
public class PluginEvent implements Serializable {

    private String bundleId;
    private String bundleVersion;

    private String what;
    private String kind;

    private EventType type;
    private Calendar date;
    private Map<String, String> parameters;

    public String getBundleId() {
        return bundleId;
    }

    public void setBundleId(String bundleId) {
        this.bundleId = bundleId;
    }

    public String getBundleVersion() {
        return bundleVersion;
    }

    public void setBundleVersion(String bundleVersion) {
        this.bundleVersion = bundleVersion;
    }

    public String getWhat() {
        return what;
    }

    public void setWhat(String what) {
        this.what = what;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public EventType getType() {
        return type;
    }

    public void setType(EventType type) {
        this.type = type;
    }

    public Calendar getDate() {
        return date;
    }

    public void setDate(Calendar date) {
        this.date = date;
    }

    public Map<String, String> getParameters() {
        return parameters;
    }

    public void setParameters(Map<String, String> parameters) {
        this.parameters = parameters;
    }

    public void addParameter(String name, String value) {
        if (parameters == null) {
            parameters = new HashMap<String, String>();
        }
        parameters.put(name, value);
    }

    public String getAsString(Map<String, String> extraAttrs) {
        StringBuilder event = new StringBuilder();
        event.append(this.getClass().getName() + " { ");
        event.append("bundle : " + getBundleId());
        event.append("; bundleVersion : " + getBundleVersion());
        event.append("; what : " + getWhat());
        event.append("; kind : " + getKind());

        event.append("; type : " + getType().name());
        event.append("; date : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getDate().getTime()));
        if (extraAttrs != null && !extraAttrs.isEmpty()) {
            StringBuilder attrs = new StringBuilder();
            for (Map.Entry<String, String> attr : extraAttrs.entrySet()) {
                attrs.append(attr.getKey() + " : " + attr.getValue() + "; ");
            }
            event.append(attrs.toString().substring(0, attrs.length() - 2));
        }
        if (getParameters() != null && !getParameters().isEmpty()) {
            StringBuilder params = new StringBuilder();
            for (Map.Entry<String, String> param : getParameters().entrySet()) {
                params.append(param.getKey() + " : " + param.getValue() + ", ");
            }
            event.append("; parameters : [" + params.toString().substring(0, params.length() - 2) + "]");
        }
        event.append(" }");
        return event.toString();
    }

    @Override
    public String toString() {
        return getAsString(null);
    }
}
